<?php

namespace App\Http\Controllers;


use App\Models\Admin;
use App\Models\Article;
use App\Models\Picture;
use App\Models\Question;
use App\Models\Reply;
use App\Models\Teacher;
use App\Models\User;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Request;

class AdminController extends BaseController
{
    public function index(){
        return view('admin/index');
    }


    public function admin(){
        return view('admin/AdminManage');
    }

    public function getAllAdmin(){
        $admins=Admin::all();
        $admin_json=json_encode($admins);
        echo $admin_json;
    }

    public function addAdmin(){
        return view('admin/addAdmin');
    }

    public function saveAdmin(Request $request){
        $admin=new Admin;
        $admin->name=$request->input('name');
        $admin->pwd=$request->input('pwd');

        $admin->save();
        return redirect()->action('AdminController@admin');
    }

    public function deleteAdmin(){
        $rows=$_POST['row'];
        foreach ($rows as $row){
            $admin = Admin::find($row['id']);
            $admin->delete();
        }
        echo 'success';
    }
    public function updateAdmin(){
        $id=$_GET['id'];
        $admin=Admin::find($id);
        return View('admin/updateAdmin')->with('admin',$admin);
    }
    public function doUpdateAdmin(Request $request){
        $admin=Admin::find($request->input('id'));
        $admin->name=$request->input('name');
        $admin->pwd=$request->input('pwd');
        $admin->save();
        return redirect()->action('AdminController@admin');
    }

    public function user(){
        return view('admin/UserManage');
    }

    public function getAllUser(){
        $users=User::where('type',0)->orderBy('created_at','DESC')->get();
        $user_json=json_encode($users);
        echo $user_json;
    }
 /*   public function addUser(){
        return view('admin/addUser');
    }
    public function saveArticle(Request $request){
        $article=new Article;
        $article->title=$request->input('title');
        $article->keywords=$request->input('keywords');
        $article->description=$request->input('description');
        $article->imgurl=$request->input('imgurl');
        $article->content=$request->input('content');

        $article->save();
        return redirect()->action('AdminController@article');
    }*/

    public function testEditor(){
        $article=Article::find(7);

        return view('index/articleDetail')->with('article',$article);
    }
    public function deleteUser(){
        $rows=$_POST['row'];
        foreach ($rows as $row){
            $user = User::find($row['id']);
            $user->delete();
        }
        echo 'success';
    }
/*    public function updateUser(){
        $id=$_GET['id'];
        $article=Article::find($id);
        return View('admin/updateArticle')->with('article',$article);
    }
    public function doUpdateArticle(Request $request){
        $article=Article::find($request->input('id'));
        $article->title=$request->input('title');
        $article->keywords=$request->input('keywords');
        $article->description=$request->input('description');
        $article->imgurl=$request->input('imgurl');
        $article->content=$request->input('content');
        $article->save();
        return redirect()->action('AdminController@article');
    }*/

    public function teacher(){
        return view('admin/TeacherManage');
    }

    public function getAllTeacher(){
        $teachers=User::where('type',1)->orderBy('created_at','DESC')->get();
        $teacher_json=json_encode($teachers);
        echo $teacher_json;
    }
/*    public function addPicture(){
        return view('admin/addPicture');
    }
    public function savePicture(Request $request){
        $picture=new Picture;
        $picture->title=$request->input('title');
        $picture->keywords=$request->input('keywords');
        $picture->description=$request->input('description');
        $picture->imgurl=$request->input('imgurl');

        $picture->save();
        return redirect()->action('AdminController@picture');
    }*/

    public function deleteTeacher(){
        $rows=$_POST['row'];
        foreach ($rows as $row){
            $teacher = User::find($row['id']);
            $teacher->delete();
        }
        echo 'success';
    }
 /*   public function updatePicture(){
        $id=$_GET['id'];
        $picture=Picture::find($id);
        return View('admin/updatePicture')->with('picture',$picture);
    }
    public function doUpdatePicture(Request $request){
        $picture=Picture::find($request->input('id'));
        $picture->title=$request->input('title');
        $picture->keywords=$request->input('keywords');
        $picture->description=$request->input('description');
        $picture->imgurl=$request->input('imgurl');
        $picture->save();
        return redirect()->action('AdminController@picture');
    }*/



    public function question(){
        return view('admin/questionManage');
    }

    public function getAllQuestion(){
        $questions=Question::orderBy('created_at','DESC')->get();
        $question_json=json_encode($questions);
        echo $question_json;
    }

    public function deleteQuestion(){
        $rows=$_POST['row'];
        foreach ($rows as $row){
            $question = Question::find($row['id']);
            $question->delete();
        }
        echo 'success';
    }

    public function reply(){
        return view('admin/replyManage');
    }

    public function getAllReply(){
        $replys=Reply::orderBy('created_at','DESC')->get();
        $reply_json=json_encode($replys);
        echo $reply_json;
    }

    public function deleteReply(){
        $rows=$_POST['row'];
        foreach ($rows as $row){
            $question = Question::find($row['id']);
            $question->delete();
        }
        echo 'success';
    }
}
